package com.zry.study.glide;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zry.study.R;
import com.zry.study.config.MyApplication;


import java.io.File;

/**
 * Created by 19428 on 2017/6/29.
 */

public class GlideLoadPhoto {


//    //加载圆形图片
//    public void  GlideCircle(Context context,Object imgUrl,  int placeholderImg, int errorImg,  ImageView view) {
//        RequestOptions options = new RequestOptions()
//                .centerCrop()
//                .placeholder(placeholderImg)
//                .error(errorImg)
//                .priority(Priority.HIGH)
//                .transform(new GlideCircleTransform());
//
//        Glide.with(context).load(imgUrl).apply(options).into(view);
//
//
//    }
//
//    //加载圆角图片
//    public void GlideRound( Context context,Object imgUrl,  int placeholderImg,int errorImg, int roundDp,ImageView view) {
//        RequestOptions options = new RequestOptions()
//                .centerCrop()
//                .placeholder(placeholderImg)
//                .error(errorImg)
//                .priority(Priority.HIGH)
//                .transform(new GlideRoundTransform(roundDp));
//
//            Glide.with(context).load(imgUrl).apply(options).into(view);
//    }
//
//    //通用加载图片
//    public void  GlideImg( Context context, Object imgUrl, int placeholderImg, int errorImg, int fallbackImg,ImageView view ) {
//        RequestOptions options = new RequestOptions().centerCrop()
//                .placeholder(placeholderImg)
//                .error(errorImg)
//                .fallback(fallbackImg);
//                Glide.with(context).load(imgUrl).apply(options).into(view);
//    }


        //加载圆形图片
        public void GlideCircle(Context context, Object imgUrl, String photoName, ImageView imageView, int errorImg) {

                File filePath = new File(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName);
                if (filePath.exists()) {//若该文件存在
                        Glide.with(context).load(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName)
                                .transform(new GlideCircleTransform(context)).error(errorImg).into(imageView);
                } else {
                        Glide.with(context).load(imgUrl)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.SOURCE)//内存缓存处理图片，磁盘缓存原图片
                                .transform(new GlideCircleTransform(context))
                                .error(errorImg).into(imageView);//加载网络图片
                        new GlideDownLoadImg().savePicture(context, photoName, imgUrl);
                }
        }


        //加载圆角图片
        public void GlideRound(Context context, Object imgUrl, String photoName, int round, ImageView imageView, int errorImg) {

                File filePath = new File(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName);
                if (filePath.exists()) {//若该文件存在
                        Glide.with(context).load(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName)
                                .transform(new GlideRoundTransform(context, round)).error(errorImg).into(imageView);
                } else {
                        Glide.with(context).load(imgUrl)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.SOURCE)//内存缓存处理图片，磁盘缓存原图片
                                .transform(new GlideRoundTransform(context, round))
                                .error(errorImg).into(imageView);//加载网络图片
                        new GlideDownLoadImg().savePicture(context, photoName, imgUrl);
                }
        }

        //通用加载图片
        public void GlideImg(Context context, Object imgUrl, String photoName, ImageView imageView, int errorImg) {

                File filePath = new File(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName);
                if (filePath.exists()) {//若该文件存在
                        Glide.with(context).load(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName)
                                .error(errorImg).into(imageView);
                } else {
                        Glide.with(context).load(imgUrl)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.SOURCE)//内存缓存处理图片，磁盘缓存原图片
                                .error(errorImg).into(imageView);//加载网络图片
                        new GlideDownLoadImg().savePicture(context, photoName, imgUrl);
                }
        }

        //只加载图片
        public void GlideLoadImg(Context context, Object imgUrl, ImageView imageView) {

                Glide.with(context).load(imgUrl).error(R.mipmap.icon_error).into(imageView);//加载网络图片
        }

        //加载视频图片
        public void loadVideoPhoto(Context context, Object imgUrl, String photoName, ImageView imageView, int errorImg) {

                File filePath = new File(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName);
                if (filePath.exists()) {//若该文件存在
                        Glide.with(context).load(MyApplication.Companion.getInstance().getPhotoSavePath() + photoName)
                                .error(errorImg).into(imageView);
                } else {
                        Glide.with(context).load(imgUrl)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.SOURCE)//内存缓存处理图片，磁盘缓存原图片
                                .error(errorImg).into(imageView);//加载网络图片
                        new GlideDownLoadImg().savePicture(context, photoName, imgUrl);
                }
        }

}
